package com.zito.manager;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.downlove.common.RequestContext;
import com.zito.bean.Admin;

public class InteractSession {
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory
			.getLogger(InteractSession.class);
	private static final String SESSION_USERNAME = "username";
	private static final String SESSION_USERID = "userid";
	private static final String SESSION_UTYPE = "usertype";
	private static final String SESSION_USERLOGIN = "online";
	private static final String SESSION_LOGINTIME = "logintime";
	private static final String JSESSION_NAME = "jsession";

	public static InteractSession get() {
		return new InteractSession();
	}

	public void setJSessionID() {
		// 时间是为豪秒为单位.
		// 比如一天时间,应该设置成:cookies.setMaxAge(24小时*60分钟*60秒*1000);
		RequestContext rc = RequestContext.get();
		rc.cookie(JSESSION_NAME, rc.session().getId(), 5 * 60 * 60 * 1000, true);
	}

	public String getJSessionID() {
		RequestContext rc = RequestContext.get();
		Cookie cookies = rc.cookie(JSESSION_NAME);
		return cookies == null ? "" : cookies.getValue();
	}

	public void recordLogin(Admin admin, String logintime) {
		setUserName(admin.getUname());
		setUserID(admin.getId());
		setUserType(admin.getType());
		setLoginTime(logintime);
		RequestContext.get().session(true).setAttribute(SESSION_USERLOGIN, 1);
	}

	public void clearLogin() {
		clearUserName();
		clearUserID();
		clearUserType();
		setUserOffLine();
	}

	public String getUserName() {
		Object tmp = RequestContext.get().sessionAttr(SESSION_USERNAME);
		return tmp == null ? "游客" : (String) tmp;
	}
	
	public String getUserName(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Object tmp = session.getAttribute(SESSION_USERNAME);
		return tmp == null ? "游客" : (String) tmp;
	}
	
	private void setUserName(String username) {
		RequestContext.get().session(true)
				.setAttribute(SESSION_USERNAME, username);
	}

	private void clearUserName() {
		RequestContext.get().session().removeAttribute(SESSION_USERNAME);
	}

	public int getUserType() {
		Object tmp = RequestContext.get().sessionAttr(SESSION_UTYPE);
		if (tmp == null) {
			return -1;
		}
		if (tmp instanceof Integer) {
			return ((Integer) tmp).intValue();
		}
		return -1;
	}
	private void setUserType(int usertype) {
		RequestContext.get().session(true).setAttribute(SESSION_UTYPE, usertype);
	}

	private void clearUserType() {
		RequestContext.get().session().removeAttribute(SESSION_UTYPE);
	}
	
	public long getUserID() {
		Object tmp = RequestContext.get().sessionAttr(SESSION_USERID);
		if (tmp == null) {
			return -1L;
		}
		if (tmp instanceof Long) {
			return ((Long) tmp).longValue();
		}
		return -1l;
	}

	private void setUserID(long userid) {
		RequestContext.get().session(true).setAttribute(SESSION_USERID, userid);
	}

	private void clearUserID() {
		RequestContext.get().session().removeAttribute(SESSION_USERID);
	}

	public boolean userIsOnline() {
		Object tmp = RequestContext.get().sessionAttr(SESSION_USERLOGIN);
		if (tmp == null) {
			return false;
		}
		if (tmp instanceof Integer) {
			return ((Integer) tmp) == 1 ? true : false;
		}
		return false;
	}

	private void setUserOffLine() {
		RequestContext.get().session().removeAttribute(SESSION_USERLOGIN);
	}

	private void setLoginTime(String times) {
		RequestContext.get().session(true)
				.setAttribute(SESSION_LOGINTIME, times);
	}

	public String getLoginTime() {
		Object tmp = RequestContext.get().sessionAttr(SESSION_LOGINTIME);
		return tmp == null ? "未记录" : (String) tmp;
	}
}
